<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2023 yoncron All rights reserved.
// +----------------------------------------------------------------------
// | Author: yoncron <yoncron@163.com>
// +----------------------------------------------------------------------
namespace app\model;

use app\common\Page;
use app\common\Util;

class FriendsModel extends ModelBase
{
    protected $name = 'base_friends';
    protected $pk = 'id';
    protected $pk_name = 'ID';
    protected $hav_deleted_column = 0;
    protected $checkField = [];

    public function friend(){
        return $this->hasOne("UserModel","user_id","friend_user_id");
    }

    public function lst($where=[],$order='add_time desc'){
        $count = $this->where($where)->count();
        $lst = $this::with('friend')->where($where)->order($order)->select();
        $lst->visible(['friend'=>['user_id','username','avatar','sex','online_status']]);
        $tree = [];
        foreach ($lst as &$val){
            if($val['remark']){
                $val['show_name'] = $val['remark'];
            }
            else{
                $val['show_name'] = $val['friend']['username'];
            }
            $val['index'] = Util::pinyin($val['show_name'],'one');
            if($val['index']==''){
                $val['index'] = "#";
            }
            else{
                $val['index'] = strtoupper($val['index']);
            }
            $tree[$val['index']][]=$val;
        }
        $res['code'] = 0;
        $res['list'] = $lst;
        $res['tree'] = $tree;
        $res['count'] = $count;
        return $res;
    }
}